#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+5;
int n,ans;
map<pair<int,int>,int> mp;
struct sss {
	int x,y;
} d[N];
vector<int> p[N];
bool vis[N];
int bfs(int S) {
	queue<pair<int,int> > q;
	q.push(make_pair(S,0));
	vis[S]=1;
	int ret=0;
	while(!q.empty()) {
		int now=q.front().first,L=q.front().second;
		ret+=L;
		q.pop();
		for(int i=0;i<p[now].size();i++) {
			if(vis[p[now][i]]) continue ;
			q.push(make_pair(p[now][i],L+1));
			vis[p[now][i]]=1;
		} 
	}
	return ret;
}
inline bool cmp1(sss x,sss y) {
	return x.y<y.y ;
}

inline bool cmp2(sss x,sss y) {
	return x.x<y.x ;
}
signed main(){
	freopen("game.in","r",stdin);
	freopen("game.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n;
	int x,y;
	int maX=0,may=0,mix=1e9,miy=1e9;
	for(int i=1;i<=n;i++) {
		cin>>x>>y;
		mp[make_pair(x,y)]=i;
		maX=max(maX,x);
		may=max(may,y);
		mix=min(mix,x);
		miy=min(miy,y);
		d[i]={x,y};
	}
	if(n<=4000) {
		for(int i=1;i<=n;i++) {
			x=d[i].x,y=d[i].y;
			if(mp[make_pair(x-1,y)]) p[i].push_back(mp[make_pair(x-1,y)]);
			if(mp[make_pair(x,y-1)]) p[i].push_back(mp[make_pair(x,y-1)]);
			if(mp[make_pair(x+1,y)]) p[i].push_back(mp[make_pair(x+1,y)]);
			if(mp[make_pair(x,y+1)]) p[i].push_back(mp[make_pair(x,y+1)]);
		}
		for(int i=1;i<=n;i++) {
			for(int j=1;j<=n;j++) vis[j]=0;
			ans+=bfs(i);
		}
		cout<<ans/2;
		return 0;
	}
	int w=maX-mix+1;
	int h=may-miy+1;
	if(h*w==n) {
		int su=0,sum=0;
		for(int i=1;i<w;i++) {
			su+=i;
			sum+=su;
		}
		ans+=sum*h*h;
		su=0,sum=0;
		for(int i=1;i<h;i++) {
			su+=i;
			sum+=su;
		}
		ans+=sum*w*w;
		cout<<ans;
		return 0;
	}
	sort(d+1,d+n+1,cmp1);
	int tot=0;
	for(int i=-1;i<=500000;i++) {
		while(tot<n&&d[tot+1].y<=i) ++tot;
		ans+=tot*(n-tot);
	}
	sort(d+1,d+n+1,cmp2);
	tot=0;
	for(int i=-1;i<=500000;i++) {
		while(tot<n&&d[tot+1].x<=i) ++tot;
		ans+=tot*(n-tot);
	}
	cout<<ans;
	return 0;
}

